package com.love_weinuo.util.timeutil;

import android.annotation.SuppressLint;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.love_weinuo.R;
import com.love_weinuo.application.LoveApplication;

/**
 * Created by Administrator
 * on 2019/2/22.
 */

public class RegisterTime extends CountDownTimer {

    private TextView tvCode;

    public RegisterTime(long millisInFuture, long countDownInterval, TextView tv) {
        super(millisInFuture, countDownInterval);
        this.tvCode = tv;
    }

    //点击之后等候过程中发生的状态
    @Override
    public void onTick(long millisUntilFinished) {
        //        tvCode.setBackgroundResource(R.drawable.draw_activity_register_message_watting);
        tvCode.setTextSize(15);
        tvCode.setText((millisUntilFinished/1000) + "s");
        tvCode.setTextColor(LoveApplication.getInstance().getResources().getColor(R.color.white));
        tvCode.setClickable(false);
    }

    //结束之后状态
    @Override
    public void onFinish() {
        //        tvCode.setBackgroundResource(R.drawable.draw_activity_register_message_code);
        tvCode.setTextSize(15);
        tvCode.setTextColor(LoveApplication.getInstance().getResources().getColor(R.color.white));
        tvCode.setText("重新获取");
        tvCode.setClickable(true);
    }

    public void onCancel() {
        Message msg = Message.obtain();
        msg.what = 1;
        handler.sendMessage(msg);
    }

    @SuppressLint("HandlerLeak")
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    cancel();
                    break;
                default:
            }
        }
    };
}
